/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.planning.tactical;

import java.awt.Dialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
import nl.cloudfarming.client.model.FieldPlan;
import nl.cloudfarming.client.planning.PlanEventKey;
import nl.cloudfarming.eventbus.BaseGuiEventProducer;
import org.jdesktop.swingx.JXPanel;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;
import org.openide.util.NbBundle;

/**
 *
 * @author Timon Veenstra
 */
public class NewFieldPlanAction extends AbstractAction {

    private SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
    private Dialog dlg;
    private final String DISPLAY_NAME = NbBundle.getMessage(this.getClass(), "field_plan.new_field_plan_action.name");
    private JTextField fieldYear = new JTextField(yearFormat.format(new Date()));
    private TacticalPlanningService service;
    private final PlanningEventProducer planningEventProducer = new PlanningEventProducer();


    public NewFieldPlanAction() {
        putValue(NAME, DISPLAY_NAME);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //
        // populate the panel with the information about what the user is about to save
        //
        JXPanel panel = new JXPanel(new MigLayout("wrap 2"));
        panel.add(new JLabel(NbBundle.getMessage(this.getClass(), "field_plan.new_field_plan_action.text")), "wrap");
        panel.add(new JLabel(NbBundle.getMessage(this.getClass(), "field_plan.new_field_plan_action.year")));
        panel.add(fieldYear);

        //
        // create the dialog and add the ok and cancel buttons
        //
        DialogDescriptor dd = new DialogDescriptor(panel, DISPLAY_NAME);
        dd.setOptions(new Object[]{getOkButton(), getCancelButton()});
        dlg = DialogDisplayer.getDefault().createDialog(dd);
        dlg.setVisible(true);
    }

    /**
     * creates the dialog ok button
     *
     * @return
     */
    private JButton getOkButton() {
        JButton jButtonOK = new JButton();
        jButtonOK.setText(NbBundle.getMessage(this.getClass(), "field_plan.new_field_plan_action.ok"));
        jButtonOK.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                dlg.setVisible(false);
                String yearText = fieldYear.getText();
                int year = Integer.parseInt(yearText);
                FieldPlan fieldPlan = getService().createFieldPlanForYear(year);
                planningEventProducer.triggerEvent(new FieldPlanEvent(fieldPlan, PlanEventKey.NEW_FIELD_PLAN));
            }
        });
        return jButtonOK;
    }

    /**
     * creates the dialog cancel button
     * @return
     */
    private JButton getCancelButton() {
        JButton jButtonCancel = new JButton();
        jButtonCancel.setText(NbBundle.getMessage(this.getClass(), "field_plan.new_field_plan_action.cancel"));
        jButtonCancel.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                dlg.setVisible(false);
            }
        });
        return jButtonCancel;
    }

    protected TacticalPlanningService getService() {
        if (service == null){
            service = new TacticalPlanningService();
        }
        return service;
    }

    public void setService(TacticalPlanningService service) {
        this.service = service;
    }
    
    private class PlanningEventProducer extends BaseGuiEventProducer {

        public PlanningEventProducer() {
            super(PlanEventKey.class);
        }
    }


}
